﻿using AutoMapper;
using MediatR;
using S.Produce.Api.Application.Commands.DeliveryCommands;
using S.Produce.Api.Application.Commands.ShiftCommands;
using S.Produce.Domains;
using S.Produce.Infrastructure;

namespace S.Produce.Api.Application.CommandHandlers.DeliveryCommandHandlers
{
    /// <summary>
    /// 修改分娩信息
    /// </summary>
    public class DeliveryUpdateCommandHandler : IRequestHandler<DeliveryUpdateCommand, int>
    {
        private readonly IRepository<Delivery> repository;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方式注入
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="mapper"></param>
        public DeliveryUpdateCommandHandler(IRepository<Delivery> repository, IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Handle(DeliveryUpdateCommand request, CancellationToken cancellationToken)
        {
            var info = await repository.GetAsync(request.DeliveryId);
            request.UpdateDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            return await repository.UpdateAsync(mapper.Map<DeliveryUpdateCommand, Delivery>(request, info));

        }
    }
}
